package com.lwl.blog.controller;

import com.alibaba.fastjson.JSON;
import com.lwl.blog.core.constants.BaseConstants;
import com.lwl.blog.core.servlet.BaseServlet;
import com.lwl.blog.entity.SystemUser;
import com.lwl.blog.service.ISystemUserService;
import com.lwl.blog.service.impl.SystemUserServiceImpl;
import org.springframework.context.ApplicationContext;

import javax.servlet.annotation.WebServlet;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/SystemUserServlet/*")
public class SystemUserServlet extends BaseServlet {
    private ISystemUserService systemUserService = new SystemUserServiceImpl();

    //从这个初始化方法中获取ioc容器，从容器中获取service对象给上面属性赋值
    @Override
    public void myInit() {
        ApplicationContext ctx = (ApplicationContext) this.req.getServletContext().getAttribute("spring");
        systemUserService = ctx.getBean("systemUserService", ISystemUserService.class);
    }

    //查询数据
    public void list() throws Exception {
        //1.查询数据
        List<SystemUser> list = systemUserService.list();
        System.out.println("list:");
        System.out.println(list);
        System.out.println("SystemUserServlet.list"+list);
        //2.将数据存入集合
        Map<String, Object> map = new HashMap<>();

        map.put("sysUserList", list);
//        System.out.println("sysUserList"+map);
        //3.跳转到试图
        outView("system/sysUserList", map);

    }

    //添加数据
    public String add() throws Exception {
        System.out.println("SystemUserServlet.add");
        //1.从前端获取数据
        String s = super.req.getReader().readLine();
        System.out.println("s = " + s);
        //2.将获得json数据转换成java
        SystemUser systemUser = JSON.parseObject(s, SystemUser.class);
        systemUser.setCreateTime(new Date());
        systemUser.setStatus(1);
        //3.然后将获得数据调用service层存入
        boolean save = systemUserService.save(systemUser);

        //返回baseservice层调用outjson处理
        return outJson(save);
    }

    //更新
    public String update() throws Exception {
        //1.获取前端数据
        String readLine = super.req.getReader().readLine();
        //2.将json字符串专为java对象
        SystemUser systemUser = JSON.parseObject(readLine, SystemUser.class);
        //3.调用service层方法通过outjson()覆盖原来的数据
        boolean save = systemUserService.update(systemUser);
        return outJson(save);

    }

    //根据id查询数据
    public void systemUserById() throws Exception {
        //1.获得id属性
        String id = this.req.getParameter("id");
        //2.调用service层得到id的相关属性
        SystemUser systemUser = systemUserService.byId(Integer.parseInt(id));
        //4.用集合将id属性存入
        Map<String, Object> map = new HashMap<>();
        map.put("systemUser", systemUser);
        //4.转发到试图处理
        outView("/system/sysUserUpdate", map);
    }

    //根据id删除
    public String delete() throws Exception {

        //获取id
//        String readLine = super.req.getReader().readLine();
        String id = super.req.getParameter("id");
        //调用service层获取id相关的方法，清除
        boolean remove = systemUserService.remove(Integer.parseInt(id));
        //转发数据到前端页面
        return BaseConstants.BASE_REDIRECT + ":" + "SystemUserServlet/list";
    }

    //批量删除
    public String batchDel () throws Exception {
        //获取前端的数据
        String readLine = req.getReader().readLine();
        //将获取的json集合转存到一个对象中
        List<Integer> list = JSON.parseArray(readLine, Integer.class);

        //返回outjson
        return outJson(systemUserService.batchRemove(list));
    }

}
